﻿using NW_WEB_Service.com.app.Interface;
using Service.src.NW.Enum;
using Service.src.NW.Model.Drive;
using BaseUtils.Helper;
using System.Collections.Generic;
using BaseUtils.Logger;
using DatabaseCore.Sqlite;

namespace NW_WEB_Service.com.app.services
{
    public class PlcService : DBService, PlcInterface
    {
        public PlcService() : base() // 确保父类构造函数执行
        {
            // 验证 DBInstance 是否有效
            if (DBInstance == null || !(DBInstance is SqliteHelper helper) || !helper.IsConnected)
            {
                LoggerService.Error("PlcService初始化失败：数据库连接无效");
            }
        }

        /// <summary>
        /// 获取PLC地址
        /// </summary>
        /// <param name="DriverType"></param>
        /// <returns></returns>
        public List<SysPlcDriverAddress> QueryByDriverType(string DriverType)
        {
            List<SysPlcDriverAddress> list = new List<SysPlcDriverAddress>();
            try
            {
                DeviceTypeEnum d = DriverType.ToEnum<DeviceTypeEnum>();
                list = DBInstance.Query<SysPlcDriverAddress>(i => i.DriverType == d);
            }
            catch (System.Exception ex)
            {
                LoggerService.Error($"获取PLC地址异常，原因：{ex.Message}", ex);
            }
            return list;
        }
    }
}
